package com.testdocs.client.signin;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.event.ProjectDeselectedEvent;
import com.testdocs.client.project.initialselect.SelectProjectPlace;

public class SignInActivity extends AbstractActivity implements SignInView.Presenter{
	private ClientFactory clientFactory;
	private SignInPlace signInPlace;
	
	public SignInActivity(SignInPlace signInPlace, ClientFactory clientFactory) {
		this.signInPlace = signInPlace;
		this.clientFactory = clientFactory;
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		clientFactory.getEventBus().fireEvent(new ProjectDeselectedEvent());

		SignInView signInView = clientFactory.getSignInView();
		signInView.setPresenter(this);
		if (signInPlace.getUserInfo() == null) {
			// Redirect to welcome page (SelectProjectPlace).
			// CheckSignInActivity will be started automatically.
			clientFactory.getPlaceController().goTo(new SelectProjectPlace());
		} else {
			signInView.setSignInLink(signInPlace.getUserInfo().getSignInUrl());
			panel.setWidget(signInView);
		}
	}
}
